package com.wx.mp.dao.impl;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wx.mp.dao.TCarSubscribeDao;
import com.wx.mp.domain.TCarSubscribe;
import com.wx.mp.domain.TCarSubscribeExample;
import com.wx.mp.mapper.TCarSubscribeMapper;
import com.wx.mp.mapper.smapper.TCarSubscribeSMapper;
import com.wx.mp.request.subscribe.oper.CarSubscribeSaveReq;
import com.wx.mp.request.subscribe.query.QuerySubscribeInfosReq;
import com.wx.mp.request.subscribe.query.QuerySubscribeInfosRes;
import com.wx.mp.utils.BizResult;
import com.wx.mp.utils.PageBean;
import com.wx.mp.utils.PageBeanUtil;
import com.wx.mp.utils.constants.DataStatusEnum;
import com.wx.mp.utils.constants.SubscribeStateEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Copyright: Copyright (c) 2017  zteits
 *
 * @ClassName: TCarSubscribeDaoImpl.java
 * @Description:
 * @version: v1.0.0
 * @date: 2019-01-12 10:08
 * Modification History:
 * Date             Author          Version            Description
 * ---------------------------------------------------------*
 */
@Component
public class TCarSubscribeDaoImpl implements TCarSubscribeDao {
    private static final Logger logger = LoggerFactory.getLogger(TCarSubscribeDaoImpl.class);

    @Autowired
    private TCarSubscribeMapper carSubscribeMapper;

    @Autowired
    private TCarSubscribeSMapper carSubscribeSMapper;

    /**
     * 预约车辆信息保存.<br/>
     * @param request
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    @Override
    public BizResult<Integer> saveTCarSubscribe(CarSubscribeSaveReq request) {
        logger.info("begin保存用户车辆预约信息...,入参={}", JSONObject.toJSON(request));
        int insertCount = 0;
        TCarSubscribe record = new TCarSubscribe();
        if(request != null ){
            BeanUtils.copyProperties(request,record);
            record.setSubscribeTime(DateUtil.parse(request.getSubscribeTime()));
            record.setDataState(DataStatusEnum.DATA_STATUS_VALID.value());
            record.setCreateDate(new Date());
            record.setSubscribeState(SubscribeStateEnum.SUBSCRIBE_SUBMIT.value());
            insertCount = carSubscribeMapper.insertSelective(record);
        }
        logger.info("end保存用户车辆预约信息...,成功插入=【{}】条数据。", insertCount);
        return new BizResult<Integer>(insertCount);
    }

    @Override
    public BizResult<PageBean<QuerySubscribeInfosRes>> querySubscribeInfos(QuerySubscribeInfosReq request) {
        PageHelper.startPage(request.getBaseRequest().getPageNum(), request.getBaseRequest().getPageSize());
        List<QuerySubscribeInfosRes> list = carSubscribeSMapper.querySubscribeInfos(request);
        PageBean<QuerySubscribeInfosRes> pageBean = new PageBean<>();
        if (CollectionUtils.isEmpty(list)) {
            return new BizResult<>(pageBean);
        }
        PageBeanUtil.copyProperties(new PageInfo<QuerySubscribeInfosRes>(list), pageBean, QuerySubscribeInfosRes.class);
        return new BizResult<>(pageBean);
    }

    @Override
    public void updateSubscribeState(Long id,Integer subscribeState) {
        TCarSubscribe record = new TCarSubscribe();
        record.setId(id);
        record.setSubscribeState(subscribeState);
        record.setModifyDate(new Date());
        carSubscribeMapper.updateByPrimaryKeySelective(record);
    }


}
